<template>
  <div class="body">
    <Form ref="form" :top="0.2" showErrorTip :model="data" :rules="validRules">
      <FormItem label="商圈名" prop="name">
        <input type="text" v-model="data.name" />
      </FormItem>
      <FormItem label="是否启用">
        <h-switch v-model="data.status" :trueValue="1" :falseValue="2"></h-switch>
      </FormItem>
      <FormItem label="所在地址">
        <Address v-model="address"></Address>
      </FormItem>
    </Form>
  </div>
</template>
<script type="text/javascript">
import Address from '@/components/address/address.vue';
export default {
  components: {
    Address
  },
  props: {
    value: [Array, Object]
  },
  data() {
    return {
      id: '',
      data: {
        name: '',
        status: 1
      },
      address:{
        province:'浙江省',
        city:'嘉兴市',
        area:'南湖区'
      },
      validRules: {
        rules: {
          name: {
            required: true
          }
        }
      }
    }
  },
  async mounted() {
    if (this.value._id) {
      this.data = JSON.parse(JSON.stringify(this.value))
      delete this.data._id
      this.id = this.value._id
      this.address.province = this.data.province
      this.address.city = this.data.city
      this.address.area = this.data.county
    }
  },
  methods: {
    /**
     * 修改绑定
     * @param  {[type]} item [description]
     * @return {[type]}      [description]
     */
    affirm(e) {
      let validResult = this.$refs.form.valid();
      if (validResult.result) {
        let { handleCancel } = e;

        this.data.province = this.address.province
        this.data.city = this.address.city
        this.data.county = this.address.area
        
        if (this.id) {
          this.$cloud.name('businessDistrict').where({ _id: this.id }).update(this.data).then(res => {
            if (res) {
              this.$Message({ type: 'success', text: "保存成功" });
              handleCancel();
            } else {
              this.$Message({ type: 'error', text: "保存失败" });
            }

          }).catch(err => {
            this.$Message({ type: 'error', text: "系统繁忙" });
          })
        } else {
          this.$cloud.name('businessDistrict').insert(this.data).then(res => {
            if (res) {
              this.$Message({ type: 'success', text: "保存成功" });
              handleCancel();
            } else {
              this.$Message({ type: 'error', text: "保存失败" });
            }

          }).catch(err => {
            this.$Message({ type: 'error', text: "系统繁忙" });
          })
        }
      }
    }
  }
};

</script>
<style type="text/css">
</style>
